﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *
 **/

using System.ComponentModel.Composition;
using System.Xml.Serialization;
using Peter.Common;
using Peter.Common.Interfaces;
using Peter.Common.MainMenu;
using Peter.Common.Utilities;

namespace Peter.NodeJs
{
   /// <summary>
   /// The options view model for the node js plugin.
   /// </summary>
   [Export (typeof (IOptionPage))]
   [PartCreationPolicy (CreationPolicy.Shared)]
   public class OptionsNodeJs : ViewModelBase, IOptionPage
   {
      private readonly INodeJs m_NodeJs;
      private string m_HttpsProxy;
      private string m_Proxy;
      private bool m_IsEnabled;

      /// <summary>
      /// Initializes a new ftp options object.
      /// </summary>
      [ImportingConstructor]
      public OptionsNodeJs (INodeJs nodeJs)
      {
         this.m_NodeJs = nodeJs;
         if (!ViewRegistry.Contains (typeof (OptionsNodeJs)))
            ViewRegistry.Register (typeof (OptionsNodeJs), typeof (ViewNodeJsOptions));
         this.GetNpmConfig();
      }

      /// <summary>
      /// Gets the configuration values for npm.
      /// </summary>
      private async void GetNpmConfig ()
      {
         this.Proxy = await this.m_NodeJs.GetNpmConfig ("proxy");
         this.HttpsProxy = await this.m_NodeJs.GetNpmConfig ("https-proxy");
         this.IsEnabled = true;
      }

      /// <summary>
      /// Gets the name of the option data.
      /// </summary>
      public string Name
      {
         get { return "NodeJs"; }
      }

      /// <summary>
      /// Loads the data from xml.
      /// </summary>
      /// <param name="xmlToLoad">Xml string to load.</param>
      public void LoadFromXml (string xmlToLoad)
      {
      }

      /// <summary>
      /// Saves any data to xml.
      /// </summary>
      /// <returns>The data to save in Xml format.</returns>
      public string SaveToXml ()
      {
         return null;
      }

      /// <summary>
      /// Gets the icon of the option page.
      /// </summary>
      [XmlIgnore]
      public MainMenuIcon OptionPageIcon
      {
         get { return MainMenuIcon.Nodejs; }
      }

      /// <summary>
      /// Gets or Sets the proxy.
      /// </summary>
      public string Proxy
      {
         get { return this.m_Proxy; }
         set
         {
            this.m_Proxy = value;
            this.OnPropertyChanged ("Proxy");
         }
      }

      /// <summary>
      /// Gets or Sets the https proxy.
      /// </summary>
      public string HttpsProxy
      {
         get { return this.m_HttpsProxy; }
         set
         {
            this.m_HttpsProxy = value;
            this.OnPropertyChanged ("HttpsProxy");
         }
      }

      /// <summary>
      /// Gets or Sets if the options are enabled.
      /// </summary>
      public bool IsEnabled
      {
         get { return this.m_IsEnabled; }
         set
         {
            this.m_IsEnabled = value;
            this.OnPropertyChanged ("IsEnabled");
         }
      }
   }
}
